package com.mintyfresh.tns.util;

/* x distance, power, wind, and mass are variables that
 * must be checked at game time.
 */

import java.io.*;
import static java.lang.System.*;
import static java.lang.Math.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.io.Serializable;

public class Physics implements Serializable{
	transient private double xPos;
	transient private double yPos;
	private double wind; 
	private double g; 
	private ArrayList<Pair> pairs;
	LinkedList<Pair> list;

	/**
	 *  Constructor
	 *
	 *  @param xPos  Double value for the x coordinate of position
	 *  @param yPos  Double value for the y coordinate of position
	 *  @param wind  Double value for power of wind in game round
	 *  @param g  Double value for gravity in game round
	 */
	public Physics(double wind, double g){
		this.pairs = new ArrayList<Pair>();
		this.wind = wind;
		this.g = g;
	} 
	/**
	 *  Enters pairs of (x, y) coordinates into an arraylist for a trajectory
	 *  path.
	 *  @param v0  Double value for the initial velocity of the projectile.
	 *  Range is from 0 to 1000
	 *  @param angle  Double value from 0 to 180 for the angle of shot.
	 */  
	public ArrayList<Pair> pairs(
			Pair origin, double v0, double angle, Terrain terrain) {
		double x;
		double y;
		double xPos = origin.x;
		double yPos = origin.y;
		double mass = 40.00; //arbit, adjust when neces.Mass     
		list = new LinkedList<Pair>();

		v0 = v0 * 2; //scaling
		angle = (double)(PI * angle)/180; //radians
		double v0x = v0 * cos(angle);
		double v0y = v0 * sin(angle);
		yPos = yPos * -1;

		for(double t = 0; ; t = t + .10) {
			x = xPos + ((v0x * t) - (0.5 * (-wind/mass) * (t*t)));     
			y = yPos + ((v0y * t) - (0.5 * (g * (t*t)))); 

			if(x >= 800 && x != xPos) {
				v0x= v0x * (-1);
				xPos = (-1 * xPos) + (1600) ;
			}

			if(y >= 0) {
				v0y = (v0y - (g * t)) * (-1);
				yPos = yPos * (-1);
			}

			if(x <= 0 && x != xPos) {
				v0x= v0x * (-1);
				xPos = -1 * xPos;
			}

			Pair pair = new Pair(x, abs(y));
			pairs.add(pair);
			list.add(pair);
			if(terrain.isBelowGround((int)x, (int)abs(y))) break;
		}
		return pairs;
			}

	public LinkedList<Pair> list(){ return list; }

	public double wind() { return wind; }

	public double g() { return g; }
}
